package co.fitstart.admin.web.view;

import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.ss.usermodel.Workbook;

import co.fitstart.admin.entity.report.ScheduleCellStyle;
import net.sf.jxls.parser.Cell;
import net.sf.jxls.processor.CellProcessor;

/**
 * 
 * @author Ysy
 *
 */
public class StyleCellProcessor implements CellProcessor {
    
    private Workbook workbook;
    
    private Map<String, HSSFCellStyle> cellStyleMaps;
    
    /**
     * 
     * @param workbook
     */
    public StyleCellProcessor(Workbook workbook){
        this.workbook = workbook;
        initCellStyle();
    }

    /*
     * (non-Javadoc)
     * @see net.sf.jxls.processor.CellProcessor#processCell(net.sf.jxls.parser.Cell, java.util.Map)
     */
    @SuppressWarnings("rawtypes")
	public void processCell(Cell cell, Map namedCells) {
        HSSFCellStyle cellStyle = cellStyleMaps.get(cell.getStringCellValue());
        if(cellStyle!=null)
            cell.getPoiCell().setCellStyle(cellStyle);
    }

    public void initCellStyle(){
        cellStyleMaps = new HashMap<String, HSSFCellStyle>(ScheduleCellStyle.values().length);
        
        for(ScheduleCellStyle style : ScheduleCellStyle.values()){
            HSSFCellStyle cellStyle = (HSSFCellStyle) workbook.createCellStyle();
            HSSFFont font = (HSSFFont) workbook.createFont();
            font.setColor(style.getFontColor());
            cellStyle.setFont(font);
            cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
            cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cellStyle.setFillForegroundColor(style.getCellColor());
            cellStyle.setFillBackgroundColor(style.getCellColor());
            
            cellStyleMaps.put(style.getDescription(), cellStyle);
        }
        
    }
    
}